package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Institution;
import com.itheima.service.InstitutionService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/institution")
public class InstitutionController {

    @Reference
    private InstitutionService institutionService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return institutionService.findPage(queryPageBean);
    }

    /**
     * 图片上传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        //获取上传文件的原始名称
        String originalFilename = multipartFile.getOriginalFilename();
        //获取文件上传后缀
        assert originalFilename != null;
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID作为上传文件名称，防止重名覆盖问题
        String fileName = UUID.randomUUID().toString() + suffix;
        Jedis resource = null;
        try {
            //使用工具类，通过字节数组上传
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(), fileName);
            //上传成功,返回结果，并把上传文件名称一并返回用于前台展示
            Result result = new Result(true, MessageConstant.UPLOAD_SUCCESS);
            result.setData(fileName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储,套餐图片所有图片名称
            resource = jedisPool.getResource();
            resource.sadd(RedisConstant.INSTITUTION_PIC_RESOURCES, fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败，返回结果信息
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }

    /**
     * 新增体检机构地址
     *
     * @param institution
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Institution institution) {
        try {
            institutionService.save(institution);
            return new Result(true, MessageConstant.ADD_INSTITUTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_INSTITUTION_FAIL);
        }
    }

    /**
     * 根据id查询体检机构
     *
     * @param institutionId
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer institutionId) {
        try {
            Institution institution = institutionService.findById(institutionId);
            return new Result(true, MessageConstant.QUERY_INSTITUTION_SUCCESS, institution);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_INSTITUTION_FAIL);
        }
    }

    /**
     * 编辑体检机构
     *
     * @param institution
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Institution institution) {
        try {
            //将参数传递给业务层处理
            institutionService.update(institution);
            return new Result(true, MessageConstant.EDIT_INSTITUTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_INSTITUTION_FAIL);
        }
    }

    /**
     * 根据id删除体检机构地址信息
     *
     * @param institutionId
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer institutionId) {
        try {
            institutionService.deleteById(institutionId);
            return new Result(true, MessageConstant.DELETE_INSTITUTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_INSTITUTION_FAIL);
        }
    }
}
